<template>
  <div class="container">
      <div class="head">
          <div class="img">
              <img src="../../assets/NJUST.jpeg" />
          </div>
          <div class="person">
              <img src="../../assets/用户名.png" />
              <li class="li1"><router-link to="/person"><span>个人主页</span></router-link></li>
          </div>
          <div class="list">
              <img src="../../assets/购物车.png" />
              <li class="li1"><router-link to="/sample"><span>我的清单</span></router-link></li>
          </div>
      </div>
      <div class="crumbs">
          <nav>
            <router-link :to="{
                 name: 'device', 
                 params: {
          staName: this.staName,
          pjName: this.pjName,
          pjId: this.pjId,
          staId:this.staId
               }
              }"><span></span></router-link>
            <router-link to="/home1"><span>首页</span><span>/</span></router-link>
               <router-link to="/home1"><span>{{ staName }}</span><span>/</span></router-link>
               <router-link :to="{
                 name: 'Home2', 
                 params: {
          param:this.staId,
          param1: this.staName,
               }
              }"><span>{{ pjName}}</span><span>/</span></router-link>
               <router-link :to="{
                 name: 'module', 
                 params: {
          staName: this.staName,
          pjName: this.pjName,
          pjId: this.pjId,
          staId:this.staId
               }
              }"><span>设备</span><span>/</span></router-link>
            <router-link :to="{
                 name: 'device', 
                 params: {
          staName: this.staName,
          pjName: this.pjName,
          pjId: this.pjId,
          staId:this.staId
               }
              }"><span>{{eName}}</span></router-link>
          </nav>
          <!--<img src="@/assets/返回.png" alt="Back"  />-->
          
      </div>
      <div class="equipment-list">
          <div class="header">设备方案列表</div>
          <el-table 
            :data="equipmentData" 
            class="responsive-table"
            :header-cell-style="{ background: '#f5f5f5' }"
            :cell-style="{ textAlign: 'center' }"
            :header-cell-class-name="'table-header'"
            v-loading="loading">
            <el-table-column 
              prop="soName" 
              label="方案名称"
              min-width="120"
              align="center">
            </el-table-column>
            <el-table-column 
              prop="picture" 
              label="仪器照片"
              min-width="120"
              align="center">
              <template slot-scope="scope">
                <div class="image-container">
                  <img 
                    :src="scope.row.picture"
                    alt="设备照片" 
                    class="thumbnail-image"
                    style="width: 80px; height: 80px;"
                    @click="handlePreview(scope.row.picture)"
                    @error="handleImageError($event)">
                </div>
              </template>
            </el-table-column>
            <el-table-column 
              prop="factory" 
              label="生产厂家"
              min-width="120"
              align="center">
            </el-table-column>
            <el-table-column 
              prop="describe" 
              label="详情"
              min-width="120"
              align="center">
              <template slot-scope="scope">
                <el-button @click="downloadPdf(scope.row)" 
                          type="text" 
                          size="small">点击查看</el-button>
              </template>
            </el-table-column>
            <el-table-column 
              label="操作"
              min-width="100"
              fixed="right"
              align="center">
              <template slot-scope="scope">
                  <el-button type="text" @click="showDialog(scope.row)">置入清单</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-dialog :visible.sync="dialogVisible" title="确认置入清单">
            <span>确将此设备置入清单吗？</span>
            <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="addToCart">确定</el-button>
            </span>
          </el-dialog>
      </div>

      <div class="chatbot">
          <img src="../../assets/客服.png" />
          <span class="chatbot-text">未找到所需内容？</span>
          <a href="#" class="chatbot-link" @click="toggleChatbot">点我留言</a>
      </div>

      <!-- 遮罩层 -->
      <div v-if="isChatbotVisible" class="chatbot-mask"></div>

      <!-- 留言弹出框 -->
      <div v-if="isChatbotVisible" class="chatbot-popup">
          <h2>留言板</h2>
          <textarea v-model="message" rows="4" cols="50" placeholder="请输入您的留言..."></textarea>
          <div class="popup-actions">
              <el-button type="primary" @click="submitMessage">提交留言</el-button>
              <el-button @click="toggleChatbot">关闭</el-button>
          </div>
      </div>

      <!-- 添加图片预览弹窗 -->
      <el-dialog
        :visible.sync="imagePreviewVisible"
        :append-to-body="true"
        class="image-preview-dialog"
        :width="'auto'"
        custom-class="preview-dialog">
        <img 
          :src="previewImageUrl"
          alt="预览图片"
          class="preview-image">
      </el-dialog>
  </div>
</template>
<script>
import pdf from 'vue-pdf';
export default {
components: { pdf },
data() {
  return {
    dialogVisible: false, // 控制置入清单弹窗显示
    isChatbotVisible: false, // 控制留言弹出框显示
    message: '', // 绑定留言内容
    staName:'',
    pjName:'',
    staId:'',
    pjId:'',
    eId:'',
    eName:'',
    selectRow:[],
    equipmentData: [
      { schemeName: '方案A', image: '', manufacturer: '厂家A', detail: '详情A' },
      { schemeName: '方案B', image: '', manufacturer: '厂家B', detail: '详情B' },
      { schemeName: '方案C', image: '', manufacturer: '厂家C', detail: '详情C' },
      // ...其他设备数据
    ],
    pdfSrc: pdf.createLoadingTask('@/assets/404_images/PDF.pdf'),
    imagePreviewVisible: false,
    previewImageUrl: '',
    loading: false, // 添加loading状态
  };
},
created:function(){
  this.staName=this.$route.params.staName;
      this.pjName=this.$route.params.pjName;
      this.eId=this.$route.params.eId;
      this.staId=this.$route.params.staId;
      this.eName=this.$route.params.eName;
      this.pjId=this.$route.params.pjId;
      console.log("输出eId："+this.eId);
      this.loading = true;
      this.$http.get("/EquipmentDetail", {
        params: {
          eId: this.eId
        }
      }).then((result) => {
        console.log(result.data);
        this.equipmentData = result.data;
      }).catch(error => {
        this.$message.error('获取数据失败，请稍后重试');
      }).finally(() => {
        this.loading = false;
      });
},
methods: {
  downloadPdf(row) {
    
    console.log("看看Url是否取到："+row.soUrl);
    fetch(row.soUrl)
    .then(response => response.blob())
    .then(blob => {
      const blobUrl = window.URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = blobUrl;
      link.download = 'certificate.pdf';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(blobUrl);
    })
    .catch(e => console.error('Error downloading the PDF:', e));
  },
 showDialog(row) {
    this.dialogVisible = !this.dialogVisible; // 显示弹窗
    // 这里可以添加其他逻辑，比如填充弹窗内容等
   this.selectRow = row;
  },
  addToCart() {
    this.$http.put('/AddEquipmentIntoOrders', {
      soId: this.selectRow.soId,
      eId: this.selectRow.eId
    })
    .then(response => {
      if(response.data === "置入成功"){
        // 添加成功提示
        this.$message({
          message: '置入清单成功',
          type: 'success',
          duration: 2000  // 提示显示2秒
        });
        console.log("置入成功");
      }
      else if(response.data === "置入清单失败"){
        // 添加失败提示
        this.$message({
          message: '置入清单失败',
          type: 'error',
          duration: 2000
        });
        console.log("置入清单失败");
      }
    })
    .catch(error => {
      // 添加错误提示
      this.$message({
        message: '操作失败，请稍后重试',
        type: 'error',
        duration: 2000
      });
      console.error('Error:', error);
    });

    this.dialogVisible = false; // 关闭确认弹窗
  },
  toggleChatbot() {
    this.isChatbotVisible = !this.isChatbotVisible; // 切换弹出框显示状态
 },
 submitMessage() {
        // 检查留言内容是否为空
        if (!this.message.trim()) {
          this.$message.error('留言内容不能为空');
          return;
        }

        // 直接发送字符串
        this.$http.post('/insertFeedback', this.message, {
          headers: {
            'Content-Type': 'text/plain'
          }
        }).then((response) => {
          // 成功提交后的处理
          this.isChatbotVisible = false; // 隐藏弹出框
          this.message = ''; // 清空留言内容

          // 显示成功提示
          this.$alert('留言成功', '提示', {
            confirmButtonText: '确定',
            type: 'success'
          });
        }).catch((error) => {
          // 处理错误情况
          console.error('留言提交失败:', error);
          
          // 根据错误状态码和响应内容给出相应提示
          if (error.response && error.response.status === 500) {
            this.$message.error('留言提交失败，请检查是否已登录');
          } else {
            this.$message.error('留言提交失败，请重试');
          }
        });
      },
  handlePreview(picture) {
    this.previewImageUrl = picture;  // 只存储图片名称
    this.imagePreviewVisible = true;
  },
  handleImageError(e) {
    e.target.src = require('@/assets/default-equipment.png');
  },
  getImageUrl(picture) {
    try {
      return require(`@/assets/${picture}.png`);
    } catch (e) {
      console.error('图片加载失败:', e);
      return require('@/assets/default-equipment.png');
    }
  }
}
};
</script>

<style scoped>
/* 整体容器样式 */
.container {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f7fa;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* 头部导航样式 */
.head {
  width: 100%;
  background-color: #abe0f3;
  height: 6vh;
}

.head .img {
  height: 100%;
  float: left;
}

.head .img img {
  height: 100%;
}

.head .list {
  float: right;
  height: 45%;
}

.head .list img {
  margin-top: 15px;
  margin-right: 7px;
  height: 100%;
  float: left;
}

.head .list li {
  margin-top: 20px;
  margin-right: 40px;
  display: block;
  float: left;
  font-size: 18px;
}

.head .person {
  float: right;
  height: 42%;
}

.head .person img {
  margin-top: 15px;
  margin-right: 7px;
  height: 100%;
  float: left;
}

.head .person li {
  margin-top: 20px;
  margin-right: 70px;
  display: block;
  float: left;
  font-size: 18px;
}

/* 面包屑导航样式 */
.crumbs {
  background-color: white;
  padding: 15px 30px;
  margin-bottom: 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.crumbs nav {
  display: flex;
  align-items: center;
  gap: 8px;
}

.crumbs a {
  color: #606266;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;
}

.crumbs a:hover {
  color: #409EFF;
}

/* 设备列表容器样式 */
.equipment-list {
  width: 90%;
  max-width: 1200px;
  margin: 20px auto;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  padding: 20px;
}

.header {
  padding: 20px;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
  border-bottom: 1px solid #ebeef5;
  margin-bottom: 20px;
}

/* 表格样式优化 */
.responsive-table {
  width: 100%;
  margin: 0 auto;
}

/* 图片容器样式 */
.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 8px;
}

.thumbnail-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
  transition: transform 0.3s;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}

.thumbnail-image:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.nav a {
  margin-left: 10px;
  text-decoration: none;
  color: #000;
}

  .nav a.active {
    background-color: #555;
    color: #fff;
    padding: 5px 10px;
    border-radius: 5px;
  }

.crumbs {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background-color: #f0f0f0;
}
.back-button {
  position: fixed;
  right: 40px;
  bottom: 40px;
  z-index: 99;
  cursor: pointer;
  transition: transform 0.3s ease;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: white;
  border-radius: 50%;
  width: 60px;
  height: 60px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.back-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.back-button img {
  height: 40px;
  width: 40px;
  object-fit: contain;
}
.equipment-list {
  width: 90%;
  max-width: 1200px;
  margin: 20px auto;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding-bottom: 20px;
}
.header {
padding: 15px 25px;
background-color: #fff;
color: #333;
font-size: 18px;
font-weight: bold;
border-bottom: 1px solid #eaeaea;
text-align: left; /* 左对齐 */
}
.pagination-centered {
text-align: center;
margin-top: 20px;
}
.chatbot {
  position: fixed;
  bottom: 20px;
  left: 20px;
  background-color:#f9f9f9 ;/* */
  padding: 10px;
  border-radius: 5px;
  display: flex;
  align-items: center; /* 垂直居中对齐 */
}
.chatbot img{
  width: 70px;
}
.chatbot-text {
  margin-right: 10px; /* 与链接间距 */
}
.chatbot-link {
  color: black; /* 设置链接颜色为黑色 */
  text-decoration: underline; /* 添加下划线 */
  font-size: 16px; /* 设置字体大小 */
  transition: color 0.3s; /* 平滑颜色变化 */
}

.chatbot-link:hover,
.chatbot-link:focus,
.chatbot-link:active {
  color: black; /* 悬停、聚焦、点击时颜色不变 */
  text-decoration: underline; /* 保持下划线 */
}
.chatbot-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5); /* 灰色半透明遮罩 */
  z-index: 999; /* 遮罩层在弹出框下层 */
}

.chatbot-popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  z-index: 1000; /* 确保弹出框在最上层 */
}
.popup-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}

.chatbot-popup h2 {
  margin-top: 0; /* 移除标题的上边距 */
}

.chatbot-popup textarea {
  width: 100%;
  margin-bottom: 10px;
}

.dialog-footer {
 text-align: right;
}

.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  padding: 0;
}

.thumbnail-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  cursor: pointer;
  border-radius: 4px;
  transition: transform 0.3s;
  margin: 0;
}
.thumbnail-image:hover {
  transform: scale(1.05);
}

.image-preview-dialog {
  display: flex;
  justify-content: center;
  align-items: center;
}

.preview-image {
  display: block;
  width: auto;
  height: auto;
  max-width: 800px;    
  max-height: 800px;   
  object-fit: contain;
}

/* 自定义弹窗样式 */
:deep(.el-dialog__body) {
  padding: 10px;
  text-align: center;
  line-height: 0;
}

.preview-dialog {
  display: inline-block;
  padding: 10px;
  background: #fff;
  border-radius: 4px;
  max-width: 90vw;  /* 最大宽度为视窗宽度的90% */
  max-height: 90vh; /* 最大高度为视窗高度的90% */
}

.preview-image {
  display: block;
  width: auto;
  height: auto;
  max-width: 800px;    /* 设置最大宽度 */
  max-height: 800px;   /* 设置最大高度 */
  object-fit: contain;
}

/* 自定义弹窗样式 */
:deep(.el-dialog) {
  margin: 0 auto !important;
  width: auto !important;
  max-width: 90vw;
}

:deep(.el-dialog__header) {
  padding: 0;
  display: none;  /* 隐藏对话框标题 */
}

:deep(.el-dialog__body) {
  padding: 10px;
  text-align: center;
  line-height: 0;  /* 消除图片底部间隙 */
}

:deep(.el-dialog__headerbtn) {
  position: absolute;
  top: -20px;
  right: -20px;
  z-index: 100;
  background: #fff;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  box-shadow: 0 0 6px rgba(0,0,0,0.3);
}

.responsive-table {
  width: 95%;
  margin: 20px auto;
  transition: all 0.3s ease;
}

/* 表格单元格通用样式 */
:deep(.el-table .cell) {
  padding: 0;
  text-align: center;
  white-space: nowrap;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

/* 表格头部样式 */
:deep(.table-header) {
  background-color: #f5f5f5 !important;
  color: #333;
  font-weight: bold;
  height: 50px !important;
}

/* 表格单元格样式 */
:deep(.el-table__cell) {
  padding: 0 !important;
  height: 80px !important;
  text-align: center !important;
}

/* 表头单元格特殊处理 */
:deep(.el-table__header .cell) {
  justify-content: center !important;
  align-items: center !important;
  display: flex !important;
  height: 50px !important;
  padding: 0 !important;
  white-space: nowrap;
}

/* 确保表头和内容列对齐 */
:deep(.el-table__header th.el-table__cell) {
  background-color: #f5f5f5 !important;
  color: #333;
  font-weight: bold;
  padding: 0 !important;
  text-align: center !important;
}

:deep(.el-table__header-wrapper .cell) {
  text-align: center !important;
  display: flex !important;
  justify-content: center !important;
  align-items: center !important;
}

/* 移除可能影响对齐的样式 */
:deep(.el-table td), 
:deep(.el-table th) {
  padding: 0 !important;
}

/* 按钮样式调整 */
:deep(.el-button--text) {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0;
}

/* 响应式布局 */
@media screen and (max-width: 1200px) {
  .equipment-list {
    width: 95%;
  }
}

@media screen and (max-width: 768px) {
  .equipment-list {
    width: 98%;
  }
  
  .responsive-table {
    width: 98%;
  }
}

/* 优化表格内容显示 */
:deep(.el-table__cell) {
  padding: 0 !important;
  height: 96px !important;
  vertical-align: middle !important;
}

:deep(.el-button--text) {
  padding: 0;
  font-size: 14px;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

/* 表格列居中对齐 */
:deep(.el-table td) {
  text-align: center !important;
}

:deep(.el-table th) {
  text-align: center !important;
}

:deep(.el-table__header .el-table__cell) {
  height: 50px !important;
  text-align: center !important;
}

:deep(.el-table__header-wrapper .cell) {
  text-align: center !important;
}


</style>